<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no, minimal-ui">
    <title>菩提阁</title>
    <link rel="icon" href="./../images/favico.ico">
    <script src="./../js/base.js"></script>
    <link rel="stylesheet" href="../../backend/plugins/element-ui/index.css" />
    <link rel="stylesheet" href="../styles/vant.min.css"/>
    <link rel="stylesheet" href="../styles/index.css" />
    <link rel="stylesheet" href="./../styles/login.css" />
</head>
<body>
<div id="login" v-loading="loading">
    <div class="divHead">登录</div>
    <div class="divContainer">
        <el-input placeholder="请输入手机号码" v-model="form.phone" maxlength='20'></el-input>
        <div class="divSplit"></div>
        <el-input placeholder="请输入验证码" v-model="form.code" maxlength='20'></el-input>
        <span class="getCode" @click='getCode'>获取验证码</span>
    </div>
    <div class="divMsg" v-if="msgFlag">手机号输入不正确，请重新输入</div>
    <el-button type="primary" :class="{btnSubmit: true, btnNoPhone: !form.phone, btnPhone: form.phone}" @click="btnLogin">登录</el-button>
</div>
<script src="../../backend/plugins/vue/vue.js"></script>
<script src="../../backend/plugins/element-ui/index.js"></script>
<script src="./../js/vant.min.js"></script>
<script src="../../backend/plugins/axios/axios.min.js"></script>
<script src="./../js/request.js"></script>
<script src="./../api/login.js"></script>
<script>
    new Vue({
        el: "#login",
        data() {
            return {
                form: {
                    phone: '',
                    code: ''
                },
                msgFlag: false,
                loading: false
            };
        },
        methods: {
            async getCode() {
                this.form.code = '';
                const regex = /^(13[0-9]{9})|(15[0-9]{9})|(17[0-9]{9})|(18[0-9]{9})|(19[0-9]{9})$/;

                if (regex.test(this.form.phone)) {
                    this.msgFlag = false;
                    // 调用后端 API 获取验证码
                    const res = await sendMsgApi({ phone: this.form.phone });
                    if (res.code === 1) {
                        this.$notify({ type: 'success', message: '验证码已发送' });
                    } else {
                        this.$notify({ type: 'warning', message: res.msg });
                    }
                } else {
                    this.msgFlag = true;
                }
            },
            async btnLogin() {
                if (this.form.phone && this.form.code) {
                    this.loading = true;
                    const res = await loginApi({ phone: this.form.phone, code: this.form.code });
                    this.loading = false;
                    if (res.code === 1) {
                        sessionStorage.setItem("userPhone", this.form.phone);
                        window.requestAnimationFrame(() => {
                            window.location.href = '/front/index.html';
                        });
                    } else {
                        this.$notify({ type: 'warning', message: res.msg });
                    }
                } else {
                    this.$notify({ type: 'warning', message: '请输入手机号码和验证码' });
                }
            }
        }
    });

    // 定义发送验证码的 API 方法
    async function sendMsgApi(param) {
        try {
            const response = await axios.post('/user/sendMsg', param); // 确保这个 URL 是正确的
            return response.data; // 返回响应数据
        } catch (error) {
            console.error(error);
            return { code: 0, msg: '发送失败' }; // 返回错误信息
        }
    }

    // 定义登录 API 方法
    async function loginApi(param) {
        try {
            const response = await axios.post('/user/login', param); // 确保这个 URL 是正确的
            return response.data; // 返回响应数据
        } catch (error) {
            console.error(error);
            return { code: 0, msg: '登录失败' }; // 返回错误信息
        }
    }
</script>
</body>
</html>
